<?php
/**
 * Controller to manage menus' items in admin area
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Content Module
 * @subpackage Controller
 * @license  New BSD License
 *
 */
class admin_BuildmenuController extends Bel_Controller_Aed
{
	/**
	 * Menu's model
	 *
	 * @var Models_Content_Menus
	 */
    private $_menus;
    
    /**
     * Pages' model
     *
     * @var Models_Content_Pages
     */
    private $_pages;
    
	/**
	 * Initialization
	 *
	 */
    function init (){
        $this->_menus = new Models_Content_Menus();
        $this->_pages = new Models_Content_Pages();
        $this->_model = new Models_Content_Page2menu();
        $this->_formname = 'menu_builder_form';
        parent::init();
    }
    
    /**
     * Displays all menus with their items
     *
     */
    public function indexAction () {
    	$this->view->assign('menus', $this->_menus->getMenusWithPages());
        $this->view->display('content/menu_builder.tpl');
    }

    /**
     * Detects if page_id param is set. Runs before menu item modifying
     *
     */
    protected function _beforeModify(){
    	if(!$this->_request->getParam('page_id')){
    		$this->_messages->setMessage('Error! Page field could not be empty','error');
    		$this->_redirect($this->_index_action);
    	}
    }
    
    /**
     * Clears menu's cache. Runs after menu item modifying 
     *
     */
    protected function _afterModify(){
    	$menu_id = $this->_request->getParam('menu_id');
    	if($menu_id){
    		$menu = $this->_menus->getEntryById($menu_id);
    		
    		if($menu){
    			Helpers_Content_Cache::clearMenuCache($menu->menu_system_name);
    		}
    	}   	
    }   
    
    /**
     * Clears menu's cache. Runs after menu item delete operation
     *
     */
    protected function _afterDelete(){
		$menu = $this->_menus->getEntryById($this->_current_entry['menu_id']);		
		if($menu){
			Helpers_Content_Cache::clearMenuCache($menu->menu_system_name);
		}
    }
    
    /**
     * Builds form to modify/add menu item
     *
     * @param Bel_Forms_Builder $form
     */
	protected function _prepareForm(Bel_Forms_Builder $form){
		$menu_id = ($this->_current_entry)?$this->_current_entry['menu_id']:$this->_request->getParam('menu_id');
		$menu = $this->_menus->getEntryById($menu_id);
		
		$form->addElement('hidden','menu_id',array('value'=>$menu_id));
		$form->addAttributes('menu_name',array('value'=>$menu->menu_system_name));
		//$form->addElement('select','page_id',array('label'=>'Page'));
		
		$pages = array();
		foreach($this->_pages->getAll() as $page){
			$pages[$page->page_id] = $page->page_title;
		}
		$form->addSubElement('page_id','options',$pages);
		
		//$form->addElement('text','menu_order',array('label'=>'Item Order'));
		//$form->addElement('submit','submit',array('value'=>'Apply'));
	}
}